package cn.iocoder.yudao.module.system.controller.admin.messageInfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.messageInfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.messageInfo.MessageInfoDO;
import cn.iocoder.yudao.module.system.convert.messageInfo.MessageInfoConvert;
import cn.iocoder.yudao.module.system.service.messageInfo.MessageInfoService;

@Api(tags = "管理后台 - 站内信")
@RestController
@RequestMapping("/system/message-info")
@Validated
public class MessageInfoController {

    @Resource
    private MessageInfoService messageInfoService;

    @PostMapping("/create")
    @ApiOperation("创建站内信")
    @PreAuthorize("@ss.hasPermission('system:message-info:create')")
    public CommonResult<Long> createMessageInfo(@Valid @RequestBody MessageInfoCreateReqVO createReqVO) {
        return success(messageInfoService.createMessageInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新站内信")
    @PreAuthorize("@ss.hasPermission('system:message-info:update')")
    public CommonResult<Boolean> updateMessageInfo(@Valid @RequestBody MessageInfoUpdateReqVO updateReqVO) {
        messageInfoService.updateMessageInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除站内信")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:message-info:delete')")
    public CommonResult<Boolean> deleteMessageInfo(@RequestParam("id") Long id) {
        messageInfoService.deleteMessageInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得站内信")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:message-info:query')")
    public CommonResult<MessageInfoRespVO> getMessageInfo(@RequestParam("id") Long id) {
        MessageInfoDO messageInfo = messageInfoService.getMessageInfo(id);
        return success(MessageInfoConvert.INSTANCE.convert(messageInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得站内信列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:message-info:query')")
    public CommonResult<List<MessageInfoRespVO>> getMessageInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<MessageInfoDO> list = messageInfoService.getMessageInfoList(ids);
        return success(MessageInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得站内信分页")
    @PreAuthorize("@ss.hasPermission('system:message-info:query')")
    public CommonResult<PageResult<MessageInfoRespVO>> getMessageInfoPage(@Valid MessageInfoPageReqVO pageVO) {
        PageResult<MessageInfoDO> pageResult = messageInfoService.getMessageInfoPage(pageVO);
        return success(MessageInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出站内信 Excel")
    @PreAuthorize("@ss.hasPermission('system:message-info:export')")
    @OperateLog(type = EXPORT)
    public void exportMessageInfoExcel(@Valid MessageInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MessageInfoDO> list = messageInfoService.getMessageInfoList(exportReqVO);
        // 导出 Excel
        List<MessageInfoExcelVO> datas = MessageInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "站内信.xls", "数据", MessageInfoExcelVO.class, datas);
    }

}
